/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.hazard.supervise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.hazard.supervise.dto.HazClassifySuperviseDTO;
import com.hopes.bizz.hazard.supervise.dto.HazClassifySuperviseExcelDTO;
import com.hopes.bizz.hazard.supervise.entity.HazClassifySuperviseEntity;
import com.hopes.bizz.hazard.supervise.entity.HazClassifySuperviseRecordEntity;
import com.hopes.bizz.hazard.supervise.service.HazClassifySuperviseRecordService;
import com.hopes.bizz.hazard.supervise.service.HazClassifySuperviseService;
import com.hopes.bizz.hazard.supervise.vo.HazClassifySuperviseByIdVO;
import com.hopes.bizz.hazard.supervise.vo.HazClassifySuperviseExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业分类监管
 *
 * @author daijie
 * @date 2024-01-29 14:42:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/classifySupervise")
@Tag(description = "classifySupervise", name = "企业分类监管管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazClassifySuperviseController {

    private final HazClassifySuperviseService hazClassifySuperviseService;
    private final HazClassifySuperviseRecordService hazClassifySuperviseRecordService;

    /**
     * 分页查询
     *
     * @param page                 分页对象
     * @param hazClassifySupervise 企业分类监管
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_view')")
    public R gethazClassifySupervisePage(Page page, HazClassifySuperviseEntity hazClassifySupervise) {
        return R.ok(hazClassifySuperviseService.page(page, hazClassifySupervise));
    }

    /**
     * 通过id查询企业分类监管
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_view')")
    public R getById(@PathVariable("id") Long id) {
        HazClassifySuperviseEntity hazClassifySuperviseEntity = hazClassifySuperviseService.getById(id);
        HazClassifySuperviseByIdVO vo = new HazClassifySuperviseByIdVO();

        BeanUtils.copyProperties(hazClassifySuperviseEntity, vo);
        List<HazClassifySuperviseRecordEntity> recordEntityList = hazClassifySuperviseRecordService.lambdaQuery()
                .eq(HazClassifySuperviseRecordEntity::getClassifySuperviseId, hazClassifySuperviseEntity.getId())
                .orderByDesc(HazClassifySuperviseRecordEntity::getCreateTime).list()
                .stream().peek(item -> {
                    item.setDateTime(item.getCreateTime());
                }).collect(Collectors.toList());
        vo.setScoreJsons(recordEntityList);
        return R.ok(vo);
    }

    /**
     * 新增企业分类监管
     *
     * @param hazClassifySupervise 企业分类监管
     * @return R
     */
    @Operation(summary = "新增企业分类监管", description = "新增企业分类监管")
    @SysLog("新增企业分类监管")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_add')")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody HazClassifySuperviseDTO hazClassifySupervise) {
        HazClassifySuperviseEntity superviseEntity = hazClassifySuperviseService.lambdaQuery().eq(HazClassifySuperviseEntity::getOrgId, hazClassifySupervise.getOrgId()).one();
        if (superviseEntity == null) {
            superviseEntity = new HazClassifySuperviseEntity();
            BeanUtils.copyProperties(hazClassifySupervise, superviseEntity);
            hazClassifySuperviseService.save(superviseEntity);
        } else {
            hazClassifySupervise.setId(superviseEntity.getId());
            hazClassifySuperviseService.updateById(hazClassifySupervise);
        }

        HazClassifySuperviseRecordEntity recordEntity = new HazClassifySuperviseRecordEntity();
        recordEntity.setClassifySuperviseId(superviseEntity.getId());
        recordEntity.setOrgId(superviseEntity.getOrgId());
        recordEntity.setScoreJson(hazClassifySupervise.getScoreJson());
        hazClassifySuperviseRecordService.save(recordEntity);

        return R.ok();
    }

    /**
     * 修改企业分类监管
     *
     * @param hazClassifySupervise 企业分类监管
     * @return R
     */
    @Operation(summary = "修改企业分类监管", description = "修改企业分类监管")
    @SysLog("修改企业分类监管")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_edit')")
    @Transactional(rollbackFor = Exception.class)
    public R updateById(@RequestBody HazClassifySuperviseDTO hazClassifySupervise) {
        hazClassifySuperviseService.updateById(hazClassifySupervise);

        HazClassifySuperviseRecordEntity recordEntity = new HazClassifySuperviseRecordEntity();
        recordEntity.setClassifySuperviseId(hazClassifySupervise.getId());
        recordEntity.setOrgId(hazClassifySupervise.getOrgId());
        recordEntity.setScoreJson(hazClassifySupervise.getScoreJson());
        hazClassifySuperviseRecordService.save(recordEntity);
        return R.ok();
    }

    /**
     * 通过id删除企业分类监管
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除企业分类监管", description = "通过id删除企业分类监管")
    @SysLog("通过id删除企业分类监管")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazClassifySuperviseService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_export')")
    public List<HazClassifySuperviseExcelVO> template() {
        return hazClassifySuperviseService.exportExcel(Collections.singletonList(new HazClassifySuperviseEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param hazClassifySuperviseExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_export')")
    public List<HazClassifySuperviseExcelVO> export(HazClassifySuperviseExcelDTO hazClassifySuperviseExcelDto) {
        return hazClassifySuperviseService.exportExcel(hazClassifySuperviseService.list());
    }

    /**
     * 导入excel 表格
     *
     * @param hazClassifySuperviseExcelDtoList 查询条件
     * @param bindingResult                    错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('haz_classifySupervise_export')")
    public R importExcel(@RequestExcel List<HazClassifySuperviseExcelDTO> hazClassifySuperviseExcelDtoList, BindingResult bindingResult) {
        return hazClassifySuperviseService.importExcel(hazClassifySuperviseExcelDtoList, bindingResult);
    }
}